package com.invite.me.ui.cehualan;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.invite.me.R;
import com.invite.me.base.BaseActivity;
import com.invite.me.beans.fabean.TabEntity;
import com.invite.me.ui.cehualan.fragment.InFormationBeiYaoyueFragment;
import com.invite.me.ui.cehualan.fragment.InFormationYaoyueFragment;
import com.invite.me.wigets.OnHomePageChangeEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

public class InformationActivity extends BaseActivity implements View.OnClickListener{



    private InFormationYaoyueFragment inFormationYaoyueFragment;
    private InFormationBeiYaoyueFragment inFormationBeiYaoyueFragment;
    private Fragment[] mFragments;
    private String[] mTitles = {"我是邀约人", "我是被邀约人"};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    CommonTabLayout commontablayout;
    private FrameLayout fragmentinformation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (null != savedInstanceState) {
            // 取出保存的Fragment
            inFormationYaoyueFragment = (InFormationYaoyueFragment) getSupportFragmentManager().getFragment(savedInstanceState,"0");
            inFormationBeiYaoyueFragment = (InFormationBeiYaoyueFragment) getSupportFragmentManager().getFragment(savedInstanceState,"1");
        }

        super.onCreate(savedInstanceState);



    }

    @Override
    protected void initView() {
        // initdata();

        if (null == inFormationYaoyueFragment) inFormationYaoyueFragment = new InFormationYaoyueFragment();
        if (null == inFormationBeiYaoyueFragment) inFormationBeiYaoyueFragment = new InFormationBeiYaoyueFragment();
        mFragments = new Fragment[]{inFormationYaoyueFragment, inFormationBeiYaoyueFragment};
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }
        commontablayout = findViewById(R.id.commontablayout);
        fragmentinformation = findViewById(R.id.fragmentinformation);
        commontablayout.setTabData(mTabEntities);
        changeFragment(0);
        commontablayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if(position==0){
                    changeFragment(0);
                }else if(position==1){

                    changeFragment(1);
                }

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        ImageView account_balance_fanhui = findViewById(R.id.account_balance_fanhui);
        account_balance_fanhui.setOnClickListener(this);

    }

    @Override
    protected int getActivityLayoutId() {
        return R.layout.activity_information;
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.account_balance_fanhui:
                finish();
                break;

        }

    }
//    public void initdata(){
//        for (int i=0;i<1;i++){
//            list.add(new InformationBean("NOTKEEP","https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3852329400,3428852864&fm=26&gp=0.jpg","2020年7月10日 23:32","相亲","女"));
//            list.add(new InformationBean("OKKEEP","https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3852329400,3428852864&fm=26&gp=0.jpg","2020年7月10日 23:32","相亲","女"));
//            list.add(new InformationBean("OKPRICE","https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3852329400,3428852864&fm=26&gp=0.jpg","2020年7月10日 23:32","相亲","女"));
//            list.add(new InformationBean("OKOK","https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3852329400,3428852864&fm=26&gp=0.jpg","2020年7月10日 23:32","相亲","女"));
//            list.add(new InformationBean("NOTKEEP","https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3852329400,3428852864&fm=26&gp=0.jpg","2020年7月10日 23:32","相亲","男"));
//            list.add(new InformationBean("OKKEEP","https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3852329400,3428852864&fm=26&gp=0.jpg","2020年7月10日 23:32","相亲","男"));
//            list.add(new InformationBean("OKPRICE","https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3852329400,3428852864&fm=26&gp=0.jpg","2020年7月10日 23:32","相亲","男"));
//            list.add(new InformationBean("OKOK","https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3852329400,3428852864&fm=26&gp=0.jpg","2020年7月10日 23:32","相亲","男"));
//
//        }
//
//
//    }




    /**
     * 根据不同的下标切换不同的界面，我的页面隐藏顶部搜索布局
     */
    private void changeFragment(int index) {
        if(index == 0){
            EventBus.getDefault().post(new OnHomePageChangeEvent());
        }
        // 切换Fragment
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < mFragments.length; i++) {
            Fragment fragment = mFragments[i];
            if (index == i) {
                if (!fragment.isAdded()) {
                    fragmentTransaction.add(R.id.fragmentinformation, fragment);
                }
                fragmentTransaction.show(fragment);
//                MobclickAgent.onEvent(this,fragmentName[i]);
            } else {
                fragmentTransaction.hide(fragment);
            }
        }
        fragmentTransaction.commit();
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        // 保存Fragment
        if (null != mFragments) {
            for (int i = 0; i < mFragments.length; i++) {
                if (null != mFragments[i] && mFragments[i].isAdded())
                    getSupportFragmentManager().putFragment(outState, String.valueOf(i), mFragments[i]);
            }
        }
        super.onSaveInstanceState(outState);

    }

}
